package com.ocom.payconfig.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.entity.master.EntityMngUserInfo;
import com.ocom.common.entity.payconfig.EntitySubsidyPlan;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.request.payconfig.AddSubsidyDataResquest;
import com.ocom.common.request.payconfig.SubsidyListRequest;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoSubsidyPlan;
import com.ocom.payconfig.service.SubsidyPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SubsidyPlanServiceImpl extends ServiceImpl<IDaoSubsidyPlan, EntitySubsidyPlan> implements SubsidyPlanService {

    @Autowired
    private IDaoSubsidyPlan iDaoSubsidyPlan;

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<EntitySubsidyPlan> subsidyList(Long comId, SubsidyListRequest subsidyListRequest) {
        if (subsidyListRequest.getSize() !=-1 && subsidyListRequest.getPage() !=-1){
            PageHelper.startPage(subsidyListRequest.getPage(),subsidyListRequest.getSize());
        }
        QueryWrapper<EntitySubsidyPlan> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("plan_id","month_value","times","status","remark",
                "DATE_FORMAT(plan_date, '%Y-%m-%d') AS planDate","clear_flag","grant_time","wallet","sub_account");
        queryWrapper.eq("com_id",comId);
        if (StringUtils.isNotEmpty(subsidyListRequest.getBeginMonth())){
            queryWrapper.ge("month_value",subsidyListRequest.getBeginMonth());
        }
        if (StringUtils.isNotEmpty(subsidyListRequest.getEndMonth())){
            queryWrapper.le("month_value",subsidyListRequest.getEndMonth());
        }
        if (StringUtils.isNotEmpty(subsidyListRequest.getStrStatus())){
            String[] arr=subsidyListRequest.getStrStatus().split(",");
            queryWrapper.in("status",arr);
        }
        if(subsidyListRequest.getSortType()==2){
            queryWrapper.orderByAsc("month_value","times");
        }else{
            queryWrapper.orderByDesc("update_time");
        }
        return iDaoSubsidyPlan.selectList(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    public List<Long> subsidyListComId(Long comId, String beginMonth) {
        QueryWrapper<EntitySubsidyPlan> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("com_id");
        queryWrapper.ge("month_value",beginMonth);
        queryWrapper.eq("status",2);
        List<EntitySubsidyPlan>  list = this.list(queryWrapper);
        if(list ==null || list.size()==0) return new ArrayList<>();
        return   list.stream().map(EntitySubsidyPlan::getComId).collect(Collectors.toList());
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public EntitySubsidyPlan getNewSubsidyPlan(Long comId, JSONObject jsonObject) {
        QueryWrapper<EntitySubsidyPlan> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",jsonObject.getLong("comId"));
        queryWrapper.eq("month_value",jsonObject.getString("month"));
        queryWrapper.eq("times",jsonObject.getInteger("times"));
        queryWrapper.last(" limit 1");
        return iDaoSubsidyPlan.selectOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Boolean newSubsidyPlan(Long comId, EntitySubsidyPlan entitySubsidyPlan) {
        return this.save(entitySubsidyPlan);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Boolean updateSubsidyPlan(Long comId, EntitySubsidyPlan updateSubsidyPlan) {
        return this.updateById(updateSubsidyPlan);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public EntitySubsidyPlan selectStatus(Long comId, String month, Integer times) {
        QueryWrapper<EntitySubsidyPlan> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("month_value",month);
        queryWrapper.eq("times",times);
        queryWrapper.in("status",2,3);
        queryWrapper.apply("plan_date <= DATE_FORMAT(now(), '%Y-%m-%d')");
        return iDaoSubsidyPlan.selectOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public void updateStatus(Long comId, Integer planId) {
        UpdateWrapper<EntitySubsidyPlan> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("status",3);
        updateWrapper.set("update_time",new Date());
        updateWrapper.eq("plan_id",planId);
        updateWrapper.eq("status",2);
        this.update(updateWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Boolean updateSubsidyPlans(Long comId, Integer planId) {
        UpdateWrapper<EntitySubsidyPlan> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("status",4);
        updateWrapper.set("update_time",new Date());
        updateWrapper.set("grant_time",new Date());
        updateWrapper.eq("plan_id",planId);
        return this.update(updateWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public EntitySubsidyPlan getRemoveSubsidyPlan(Long comId, AddSubsidyDataResquest addSubsidyDataResquest) {
        QueryWrapper<EntitySubsidyPlan> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",addSubsidyDataResquest.getComId());
        queryWrapper.eq("month_value",addSubsidyDataResquest.getMonth());
        queryWrapper.eq("times",addSubsidyDataResquest.getTimes());
        queryWrapper.eq("status",1);
        queryWrapper.last(" limit 1");
        return iDaoSubsidyPlan.selectOne(queryWrapper);
    }
}
